package com.yuyou.hxyy.gateway.controller;

import com.yuyou.hxyy.service.api.bean.LoginRedisObj;
import com.yuyou.hxyy.service.api.bean.PageParams;
import com.yuyou.hxyy.service.api.bean.Pagination;
import com.yuyou.hxyy.service.api.bean.ReturnResult;
import com.yuyou.hxyy.service.api.entity.WordUnit;
import com.yuyou.hxyy.service.api.security.SecurityCons;
import com.yuyou.hxyy.service.api.service.IWordUnitService;
import com.yuyou.hxyy.service.api.vo.resp.WordUnitStatisticsVo;
import com.yuyou.hxyy.service.api.vo.resp.WordUnitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="WordUnitController",description="关卡接口")
@Validated
@RestController
@RequestMapping(value="/api/wordUnit")
public class WordUnitController extends BaseController{

        @Autowired
        private IWordUnitService wordUnitService;


        @ApiOperation("插入关卡")
        @PreAuthorize(SecurityCons.admin)
        @RequestMapping(value ="/addWordUnit",method = {RequestMethod.POST})
        public ReturnResult<Void> addWordUnit(@ApiParam(value = "类别")@NotNull(message = "类别不能为空")@RequestParam(value = "category")Integer category,
                                              @ApiParam(value = "词库ID")@NotNull(message = "词库ID不能为空")@RequestParam(value = "wordRepoId")Long wordRepoId,
                                              @ApiParam(value = "关卡名称")@NotBlank(message = "关卡名称不能为空")@RequestParam(value = "name") String name,
                                              @ApiParam(value = "序号")@RequestParam(value = "sort" , required = false) Integer sort,
                                              HttpServletRequest request){
            WordUnit wu = wordUnitService.findByNameRepo(wordRepoId , name);
            if(wu != null){//该词典已存在该名称的关卡
                return ReturnResult.fail("该词典已存在该名称的关卡");
            }
            LoginRedisObj user = this.getUserObj(request);
            wordUnitService.insertWordUnit(name , wordRepoId , category , user.getUserId() , sort);
            return ReturnResult.success("OK");
        }

        @ApiOperation("更新关卡")
        @PreAuthorize(SecurityCons.admin)
        @RequestMapping(value ="/updateWordUnit",method = {RequestMethod.POST})
        public ReturnResult<Void> updateWordUnit(@ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id")Long id,
                                                 @ApiParam(value = "关卡名称")@NotBlank(message = "关卡名称不能为空")@RequestParam(value = "name") String name,
                                                 @ApiParam(value = "序号")@RequestParam(value = "sort" , required = false) Integer sort,
                                              HttpServletRequest request){
            LoginRedisObj user = this.getUserObj(request);
            wordUnitService.updateWordUnit(name , id , user.getUserId() , sort);
            return ReturnResult.success("OK");
        }

        @ApiOperation("通过ID获取关卡数据")
        @RequestMapping(value ="/getWordUnitById",method = {RequestMethod.GET, RequestMethod.POST})
        public ReturnResult<WordUnit> getWordUnitById(@ApiParam(value = "id")@NotNull(message = "ID不能为空")@RequestParam(value = "id")Long id){
            WordUnit wr = wordUnitService.selectByPrimaryKey(id);
            return ReturnResult.success(wr);
        }

        @ApiOperation("批量删除关卡")
        @PreAuthorize(SecurityCons.admin)
        @RequestMapping(value ="/delWordUnit",method = {RequestMethod.POST})
        public ReturnResult<Void> delWordUnit(@ApiParam(value = "关卡id，多个用逗号隔开")@NotBlank(message = "id不能为空")@RequestParam(value = "ids") String ids){

            wordUnitService.deleteByIds(ids);
            return ReturnResult.success("OK");
        }

    @ApiOperation("关卡接口分页查询")
    @RequestMapping(value ="/findWordUnitPage",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<WordUnitVo> findWordUnitPage(@Validated @ModelAttribute PageParams pageParams,
                                                     @ApiParam(value = "类别")@RequestParam(value = "category" , required = false)Integer category,
                                                     @ApiParam(value = "关卡名称")@RequestParam(value = "name" , required = false) String name,
                                                     @ApiParam(value = "词典ID")@RequestParam(value = "wordRepoId" , required = false) Long wordRepoId){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<WordUnitVo> queryPgn = new Pagination<>();
        queryPgn.setPageParams(pageParams);
        if(category != null){
            conditions.put("category", category);
        }
        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim().replace("%", "\\%").replace("_", "\\_"));
        }
        if(!StringUtils.isEmpty(wordRepoId)){
            conditions.put("wordRepoId", wordRepoId);
        }
        queryPgn.setConditions(conditions);
        int total = wordUnitService.findWordUnitCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<WordUnitVo> list = wordUnitService.findWordUnitPage(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }

    /*@ApiOperation("词典下拉数据")
    @PreAuthorize(SecurityCons.admin)
    @RequestMapping(value ="/findAllWordUnit",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<WordUnitVo> findAllWordUnit(@Validated @ModelAttribute PageParams pageParams,
                                                    @ApiParam(value = "类别")@RequestParam(value = "category" , required = false)Integer category,
                                                    @ApiParam(value = "词库名称")@RequestParam(value = "name" , required = false) String name,
                                                    @ApiParam(value = "词典ID")@RequestParam(value = "wordRepoId" , required = false) Long wordRepoId){
        Map<String, Object> conditions = new HashMap<>();
        if(category != null){
            conditions.put("category", category);
        }
        if(!StringUtils.isEmpty(name)){
            conditions.put("name", name.trim());
        }
        if(!StringUtils.isEmpty(wordRepoId)){
            conditions.put("wordRepoId", wordRepoId);
        }
        List<WordUnitVo> list = wordUnitService.findAllWordUnit(conditions);
        return ReturnResult.success(list);
    }*/



    @ApiOperation("关卡学习列表")
    @RequestMapping(value ="/getUnitList",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<List<WordUnitStatisticsVo>> getUnitList(@ApiParam(value = "词典ID")@NotNull(message = "词典ID不能为空")@RequestParam(value = "wordRepoId") Long wordRepoId,
                                                                HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        List<WordUnitStatisticsVo> list = wordUnitService.getUnitList(wordRepoId , user.getUserId());
        return  ReturnResult.success(list);
    }

    @ApiOperation("关卡通过")
    @RequestMapping(value ="/passUnit",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<Void> passUnit(@ApiParam(value = "词典ID")@NotNull(message = "词典ID不能为空")@RequestParam(value = "wordRepoId") Long wordRepoId,
                         @ApiParam(value = "关卡ID")@NotNull(message = "关卡ID不能为空")@RequestParam(value = "wordUnitId") Long wordUnitId,
                         HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        wordUnitService.passUnit(wordRepoId , wordUnitId , user.getUserId());
        return ReturnResult.success("OK");
    }
}
